<template>
	<view style="background-color: #fff;">
		<!-- 轮播图 -->
		<uni-swiper-dot style="background-color: #fff;" class="uni-swiper-dot-box" :info="swiperData" :current="current" field="content">
			<swiper autoplay :circular="true" interval="6000" class="swiper-box" :current="swiperDotIndex"
				@change="changeSwiper">
				<swiper-item v-for="(item, index) in swiperData" :key="index">
					<view @click="toAdUrl(item)" class="swiper-item">
						<image :src="item.showimg" mode="" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>
		<view style="background-color: #fff;margin-top: 16rpx;" class="page_box mb12">
			<button class="bf_input_down pad" open-type="share" form-type="submit">
				<image class="bf_input_down" src="/static/image/index/fx.png" mode=""></image>
			</button>
			<view class="notice-box">
				<image class="notice-svg" src="/static/image/index/zt.svg" mode=""></image>
				<view class="notice-text">
					<p>{{sysConfig.sysConfig.gg}}</p>
				</view>
			</view>
			<view class="inputmain">
				<view class="weui-cells" style="width:400rpx;">
					<u-input placeholder="视频或图集链接" class="weui-input" v-model="value" :type="type" height="90" />
				</view>
				<view class="ztbut" @click="zt()">粘贴</view>
				<view class="jiexibut" @click="query()">解析</view>
			</view>


			<view>
				<image @click="toTab('/pages/recommend/help')" class="banner" src="/static/image/index/zuoyi.png">
				</image>
				<image @click="toTab('/pages/recommend/share')" class="banner" src="/static/image/index/youyi.png">
				</image>
				<image @click="toTab('/pages/recommend/faq')" class="banner" src="/static/image/index/zuoer.png">
				</image>
				<image @click="toTab('/pages/recommend/md5faq')" class="banner" src="/static/image/index/youer.png">
				</image>
			</view>

			<image class="jiaocheng" src="/static/image/index/jiexi4.jpg" mode=""></image>
		</view>
	</view>
</template>

<script>
	let interstitialAd = null
	import {
		delShuiYin,
		autoLoginWx,
		lunList
	} from "@/api/api.js"
	import config from '@/config'
	export default {
		data() {
			return {
				swiperDotIndex: 0,
				current: 0,
				baseUrl: config.baseUrl,
				swiperData: [{
					showimg: 'https://simgs.emoboy.vip/app-show/shuiyin/qsyfm1.png'
				}],
				value: '',
				type: 'text',
				userName: config.userName,
				openId: null,
				sysConfig: null
			}
		},
		onShareAppMessage() {
			return {
				title: this.sysConfig.sysConfig.fxTitle,
				imageUrl: this.sysConfig.sysConfig.fxUrl,
				path: 'pages/index?userid=' + this.sysConfig.userId,
			}
		},
		onShareTimeline() {
			return {
				title: this.sysConfig.sysConfig.fxTitle,
				query: {},
				imageUrl: this.sysConfig.sysConfig.fxUrl
			}
		},
		onLoad(option) {
			//console.log(option)
			var that = this;
			lunList({
				userName:that.userName
			}).then(response => {
				if(response.rows.length != 0){
					for(var i=0;i<response.rows.length;i++){
						response.rows[i].showimg = that.baseUrl+response.rows[i].showimg
					}
					that.swiperData = response.rows
				}
			})
			var oldUserId = 0;
			//邀请用户
			if (option.userid && option.userid != 'undefined') {
				oldUserId = option.userid
			}
			//自动登录
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					//获取openid
					autoLoginWx({
						code: loginRes.code,
						user: that.userName,
						oldUserId: oldUserId
					}).then(response => {
						uni.setStorageSync("appUser", response.data)
						that.sysConfig = response.data
						//console.log(that.sysConfig)
						that.openId = response.data.openId
						if (that.sysConfig.sysConfig.cpAd) {
							if (wx.createInterstitialAd) {
								interstitialAd = wx.createInterstitialAd({
									adUnitId: that.sysConfig.sysConfig.cpAd
								})
								interstitialAd.onLoad(() => {})
								interstitialAd.onError((err) => {
									console.error('插屏广告加载失败', err)
								})
								interstitialAd.onClose(() => {})
							}
						}
						// 在适合的场景显示插屏广告
						if (interstitialAd) {
							setTimeout(() => {
								interstitialAd.show().catch((err) => {
									console.error('插屏广告显示失败', err)
								})
							}, 1000); // 2000毫秒后执行

						}

					})

				}
			});
		},
		methods: {
			toAdUrl(val) {
				uni.navigateToMiniProgram({
					appId: val.appid,
					path: val.urls, //需要打开的目标路径
					extraData: {},
					envVersion: 'release', //小程序版本：develop（开发版），trial（体验版），release（正式版）
					success(res) {
						// 打开成功
					}
				})
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			query() {
				let _this = this;
				if (_this.value == "" || _this.value == null) {
					uni.showToast({
						title: '请填写内容后解析哦！',
						icon: 'none',
						duration: 2000
					});
				} else {
					let getUrl = _this.getStrUrl(_this.value)
					if (getUrl == null) {
						uni.showToast({
							title: '糟糕！您填写的内容找不到解析地址，无法解析！',
							icon: 'none',
							duration: 2000
						});
						_this.value = ''
						return;
					}
					_this.startJx(getUrl)
				}
			},
			startJx(getUrl) {

				let _this = this;
				uni.showLoading({
					title: '解析中...',
					mask: true
				});
				delShuiYin({
					url: getUrl,
					key: _this.userName,
					openId: _this.openId
				}).then(response => {
					if (response.data.code == 404) {
						uni.showModal({
							title: '提示',
							content: response.data.msg,
							showCancel: false,
							confirmText: "确定",
							success: function(res) {
								if (res.confirm) {
									// 执行确认后的操作
								} else {
									// 执行取消后的操作
								}
							}
						})
						uni.hideLoading()
						return
					}
					var dataMsg = response.data.data_info
					uni.hideLoading()
					if (dataMsg.code != 200) {
						uni.showModal({
							title: '提示',
							content: dataMsg.msg,
							showCancel: false,
							confirmText: "确定",
							success: function(res) {
								if (res.confirm) {
									// 执行确认后的操作
								} else {
									// 执行取消后的操作
								}
							}
						})
					} else {
						uni.setStorageSync("analysisData", dataMsg)
						_this.value = ''
						if (dataMsg.pics == "") {
							_this.$tab.navigateTo("/pages/common/video")
						} else {
							_this.$tab.navigateTo("/pages/common/image")
						}


					}

				})
			},
			getStrUrl(s) {
				var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
				var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
				s = s.match(reg);
				return (s && s.length ? s[0] : null);
			},
			toTab(url) {
				this.$tab.navigateTo(url)
			},
			zt() {
				uni.getClipboardData({
					success: function(res) {
						this.value = res.data; // 更新数据到组件状态
					}.bind(this) // 确保在箭头函数中正确绑定this上下文
				});
			}
		}

	}
</script>

<style lang="scss" scoped>
	.notice-box {
		width: 100%;
		margin-top: 20rpx;
		margin-bottom: 0rpx;
	}

	.notice-ioc {
		height: 40upx;
		width: 120upx;
		margin-left: 20rpx;
		display: inline-block;
		float: left;
	}

	.notice-svg {
		height: 50upx;
		width: 100upx;
		display: inline-block;
		float: left;
		margin-top: -1rpx;
		margin-left: 20rpx;
	}

	.notice-text {
		display: inline-block;
		margin-left: 5rpx;
		margin-top: 8rpx;
		width: 555upx;
		height: 35rpx;
		overflow: hidden;
		white-space: nowrap;
		box-sizing: border-box;
		border-radius: 20rpx;
	}

	.notice-text p {
		display: inline-block;
		padding-left: 100%;
		animation: marquee 8s linear infinite;
	}

	@keyframes marquee {
		0% {
			transform: translateX(0);
		}

		100% {
			transform: translateX(-100%);
		}
	}

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff !important;
		min-height: 100%;
		height: auto;
	}

	button::after {
		border: none;
	}

	.head {
		width: 100%;
		height: 400px;
	}

	.bf_input_down {
		width: 100%;
		height: 166rpx;
		background-color: #fff;
		background-size: 103% 100%;
		padding-left: 0;
		padding-right: 0;
	}

	.swiper {
		height: 450rpx;
	}

	.swiper-box {
		height: 345rpx;
		overflow: hidden;
		transform: translateY(0);
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 345rpx;
		line-height: 345rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}

	.inputmain {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 10px 0 15px 0;
		border: none;
		background-color: #fff;

		.weui-cells {
			position: relative;
			background-color: #f3f3f3;
			line-height: 1.41176471;
			font-size: 14px;

			.weui-cell {

				.weui-cell__bd {
					flex: 1;

					.weui-input {
						line-height: 43px;
						font-family: "iconfont" !important;
					}
				}
			}

			.weui-cell_input {
				padding-top: 0;
				padding-bottom: 0;
			}
		}
	}

	.ztbut {
		display: inline-flex;
		justify-content: center;
		align-items: center;
		width: 120rpx;
		height: 74rpx;
		background: linear-gradient(rgb(253, 230, 190), rgb(255, 172, 191));
		color: #333;
		font-size: 30rpx;
		border: none;
		border-right: 1px solid #f3f3f3
	}

	.jiexibut {
		display: inline-flex;
		justify-content: center;
		align-items: center;
		width: 126rpx;
		height: 74rpx;
		background: linear-gradient(rgb(253, 230, 190), rgb(255, 172, 191));
		/* background: linear-gradient(#ffd800, #fb0); */
		color: #333;
		font-size: 30rpx;
		border-top-right-radius: 10rpx;
		border-bottom-right-radius: 10rpx;
		border: none
	}

	.banner {
		position: relative;
		float: left;
		padding: 5px 0px;
		width: 50%;
		height: 170rpx;
		box-sizing: border-box;
		border-right: 0rpx solid #D9D9D9;
		border-bottom: 0rpx solid #D9D9D9;
	}

	.jiaocheng {
		margin-left: 45rpx;
		margin-bottom: 10rpx;
		margin-right: 45px;
		display: block;
		width: 100%;
		height: 800rpx;
		background-size: 100% 100%;
		margin: 10px auto;
		line-height: 36px;
		text-align: center;
		font-size: 18px;
	}

	.mp4 {
		width: 90%;
		margin-left: 5%;
		margin-right: 5%;
	}
</style>